package com.zxisl.util;

import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;

/**
 * Created by ade on 2016/10/31.
 */
public class FileUtil {

    public static Logger fileUtilLogger = Logger.getLogger(FileUtil.class);



    /**
     * 写入邮件所有内容到文件
     *
     * @param gson
     * @param file
     * @param mailInfoMap
     */
    public static void writeMessageToFile(Gson gson, File file, HashMap<String, Object> mailInfoMap) {
        try {
            writeFile(file, gson.toJson(mailInfoMap), Charset.defaultCharset(), true);
            FileUtils.write(file, "\r\n", Charset.defaultCharset(), true);
        } catch (IOException e) {
            fileUtilLogger.info(e.getMessage());
        }
    }

    /**
     * 写两个数组的数据到一个文件中去
     *
     * @param file
     * @param str
     * @param charset
     * @param append
     * @throws IOException
     */
    public static void writeFileOfTwoAttr(File file, String[] str, Charset charset, boolean append) {
        try {
            System.out.println("正在写入： " + str[0]);
            FileUtil.writeFile(file, str[0], charset, append);
            FileUtils.write(file, str[1],charset, append);
            FileUtils.write(file, "\r\n", charset, append);
        } catch (Exception e) {
            fileUtilLogger.info(e.getMessage());
        }
    }

    /**
     * 写数据
     *
     * @param file
     * @param content
     * @param charset
     * @param append
     * @throws IOException
     */
    public static void writeFile(File file, String content, Charset charset, boolean append) {
        try {
            FileUtils.write(file, content, charset, append);
            FileUtils.write(file, ",", charset, append);
        } catch (Exception e) {
            fileUtilLogger.info(e.getMessage());
        }
    }
}
